# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html

import json

class MtimespiderPipeline(object):
    '''Item数据输出JSON文件的Pipeline类'''

    def __init__ (self):
        '''初始化要写入的JSON文件'''
        self.json_file = open('topmovies.json', 'wb+')
        self.json_file.write('\n'.encode('utf-8'))

    def process_item(self, item, spider):
        ''' 
            将item转化为json格式并写入json文件
            item参数就是spider yield所返回的item对象
        '''
        # 将每个item对象转化成一个json字符串
        text = json.dumps(dict(item), ensure_ascii=False) + ',\n'
        self.json_file.write(text.encode('utf-8'))

    def close_spider (self, spider):
        ''' 
            spider关闭时（自动调用close_spider方法），关闭json文件
        '''
        print('----------关闭文件----------')
        self.json_file.seek(-2, 1)
        self.json_file.write('\n'.encode('utf-8'))
        self.json_file.close()
